package dth.dao;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.management.Query;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

import dth.entity.Account;
import dth.entity.BookMovie;
import dth.utils.DthUtil;

public class BookMovieDAO {
	private SessionFactory sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
	private Session session=sessionFactory.openSession();
	public List<BookMovie> getListBookMovie(int userId) { // list book chua thanh toan
		session.beginTransaction();
		String queryString = "Select * from book_movie where fk_user_id=:fk_user_id and status=0";
		SQLQuery query = session.createSQLQuery(queryString).addEntity(BookMovie.class);
		System.out.println("id user " +userId);
		query.setParameter("fk_user_id", userId);///////////////////////////
		return query.list();
	}
	
	public List<BookMovie> getListBookMovie2(int userId,int idmovie) { // list book chua thanh toan kèm theo id film book
		
		String queryString = "Select * from book_movie where fk_user_id=:fk_user_id and status=0 and fk_movie_id=:fk_movie_id";
		SQLQuery query = session.createSQLQuery(queryString).addEntity(BookMovie.class);
		query.setParameter("fk_movie_id", idmovie);
		query.setParameter("fk_user_id", userId);///////////////////////////
		return query.list();
	}
	
	public List<BookMovie> getListBookMovie3() //Lấy tất cả các order movie chưa thanh toán
	{
		session.beginTransaction();
		String queryString = "Select * from book_movie where status=0";
		SQLQuery query = session.createSQLQuery(queryString).addEntity(BookMovie.class);
		return query.list();
	}
	
	public List<BookMovie> getListBookMovie4() //Lấy tất cả các order movie đã thanh toán
	{
		session.beginTransaction();
		String queryString = "Select * from book_movie where status=1";
		SQLQuery query = session.createSQLQuery(queryString).addEntity(BookMovie.class);
		return query.list();
	}
	
	public List<BookMovie> getAllListBookMovie() { // all list bookmovie
		session.beginTransaction();
		String queryString = "Select * from book_movie";
		SQLQuery query = session.createSQLQuery(queryString).addEntity(BookMovie.class);
		return query.list();
	}
	public int intsert(BookMovie m)
	{
		if(checkInsert(m)) // kiem tra xem ngay` do da~ dat film nao giong vay chua
		{
			return 2;
		}
		else
		{
			Transaction tx=session.beginTransaction();
			String queryString ="Insert into book_movie(fk_user_id,fk_movie_id,date_insert,status,movie_time) Values(:fk_user_id,:fk_movie_id,:date_insert,0,:movie_time)";
			SQLQuery query=session.createSQLQuery(queryString);
			query.setParameter("fk_user_id", m.getFkUserId());
			query.setParameter("fk_movie_id", m.getFkMovieId());
			query.setParameter("date_insert", m.getDateInsert());
			//query.setParameter("quantity", m.getQuantity());
			query.setParameter("movie_time", m.getMovieTime());
			System.out.println("fk_user_id "+m.getFkUserId() +"fk_movie_id " + m.getFkMovieId());
			int count=query.executeUpdate();
			tx.commit();
			return count;
		}
	}
	
	public Boolean checkInsert(BookMovie m)
	{
		
		List<BookMovie> lst = new ArrayList<BookMovie>();
		lst = getListBookMovie2(m.getFkUserId(), m.getFkMovieId());
		java.util.Date date=new java.util.Date();//lay ngay hien tai
		
		for (BookMovie bookMovie : lst) {
			date =bookMovie.getMovieTime();
			System.out.println(date.getDate() + "ht -- book " + m.getMovieTime().getDate());
			if(date.getDate()== m.getMovieTime().getDate() && date.getMonth() == m.getMovieTime().getMonth() && date.getYear()== m.getMovieTime().getYear())
			{
				return true;
			}
		}
//		String queryString="select count(*) from book_movie where fk_user_id=:fk_user_id and fk_movie_id=:fk_movie_id and status=0";
//		SQLQuery query=session.createSQLQuery(queryString);
//		query.setParameter("fk_user_id",m.getFkUserId());
//		query.setParameter("fk_movie_id",m.getFkMovieId());
//		if(query.list().get(0).toString() == "0")
//			return false;
		return false;
	}
	
	public int updateStatus(int id) // id cua user
	{
		System.out.println("update movie status with id: "+id);
		Transaction tx=session.beginTransaction();
		String qureyString ="Update book_movie set status=1 where fk_user_id=:fk_user_id and status=0";
		SQLQuery query = session.createSQLQuery(qureyString);
		query.setParameter("fk_user_id", id);
		query.executeUpdate();
		tx.commit();
		int count = query.executeUpdate();
		return count;
	}

	public int delete(int id) {
		Transaction tx=session.beginTransaction();
		String queryString="delete from book_movie where id= :id";
		SQLQuery query = session.createSQLQuery(queryString);
		query.setParameter("id", id);
		int count= query.executeUpdate();
		tx.commit();
		return count;
	}
	
	
}
